package edu.colorado.epmapp;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import edu.colorado.epmapp.models.EPMDefs;
import edu.colorado.epmapp.models.User;
import edu.colorado.epmapp.network.HomeNetwork;
import edu.colorado.epmapp.network.OnResponseListener;

public class LoginActivity extends Activity{

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        final Context context = this;
        
        Button loginBtn = (Button) findViewById(R.id.loginBtn);
        Button createAccountBtn = (Button) findViewById(R.id.createAccountBtn);
        Button facebookLoginBtn = (Button) findViewById(R.id.facebookLoginBtn);
        
        //TODO - GET RID OF THESE LINES WHEN ACCOUNT CREATION AND FB LOGIN IS READY
        createAccountBtn.setEnabled(false);
        facebookLoginBtn.setEnabled(false);
        
        final EditText usernameText = (EditText) findViewById(R.id.usernameText);
        final EditText passwordText  = (EditText) findViewById(R.id.passwordText);
        
        // Set the max length of the username and password fields. Filters can extend for more
        // input checking in the future. 
        InputFilter usernameFilter = new InputFilter.LengthFilter(EPMDefs.USER_USERNAME_LENGTH);
        InputFilter passwordFilter = new InputFilter.LengthFilter(EPMDefs.USER_PASSWORD_LENGTH);
        usernameText.setFilters(new InputFilter[]{usernameFilter});
        passwordText.setFilters(new InputFilter[]{passwordFilter});
        
        //DEBUG CREDENTIALS
        usernameText.setText("test");
        passwordText.setText("test");
        
        loginBtn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				String username = usernameText.getText().toString();
				String password = passwordText.getText().toString();
				if(username == null || username.equals("")){
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
					alertDialogBuilder.setTitle("Missing Username");
					alertDialogBuilder.setMessage("A username is required.");
					alertDialogBuilder.setCancelable(true);
					alertDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	dialog.cancel();
	                    }
	                });
					AlertDialog alert = alertDialogBuilder.create();
					alert.show();
					//TODO - "|| password.length() < PASSWORD_MIN_LENGTH" 
				}else if(password == null || password.equals("")){
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
					alertDialogBuilder.setTitle("Missing Password");
					alertDialogBuilder.setMessage("A password is required.");
					alertDialogBuilder.setCancelable(true);
					alertDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	dialog.cancel();
	                    }
	                });
					AlertDialog alert = alertDialogBuilder.create();
					alert.show();
				}else{
					User.getUser().setUsername(username);
					User.getUser().setPassword(password);
					HomeNetwork hn = new HomeNetwork(null);
					hn.login( new OnResponseListener() {
						
						public void onRequestComplete(String response) {
							if(response != null && response.contains("200")){
								Intent submitScreen = new Intent(getApplicationContext(), FormOneActivity.class);
								startActivity(submitScreen);
							}else if(response == null)
							{
								AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
								alertDialogBuilder.setTitle("Sorry");
								alertDialogBuilder.setMessage("There may be a network problem. Please try again.");
								alertDialogBuilder.setCancelable(true);
								alertDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				                    public void onClick(DialogInterface dialog, int which) {
				                    	dialog.cancel();
				                    }
				                });
								AlertDialog alert = alertDialogBuilder.create();
								alert.show();
							}
							else{
								AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
								alertDialogBuilder.setTitle("Login error");
								alertDialogBuilder.setMessage("Incorrect username or password.");
								alertDialogBuilder.setCancelable(true);
								alertDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				                    public void onClick(DialogInterface dialog, int which) {
				                    	dialog.cancel();
				                    }
				                });
								AlertDialog alert = alertDialogBuilder.create();
								alert.show();
							}
							
						}
					});
				}
			}
		});
        
        createAccountBtn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent createAccountScreen = new Intent(getApplicationContext(), CreateAccountActivity.class);
				startActivity(createAccountScreen);
			}
		});
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
}
